package com.jp.knowledge.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.TextView;

import com.google.gson.JsonObject;
import com.jp.knowledge.R;
import com.jp.knowledge.fragment.SearchDetailFragment;
import com.jp.knowledge.util.JsonUtil;
import com.jp.knowledge.view.JpApplicationWebView;

import org.xutils.view.annotation.ViewInject;

import java.io.Serializable;


/**
 * Created by 528787067@qq.com on 2017/1/20.
 */

public class SearchSimpleActivity extends SearchActivity {

    public static void gotoSearch(Context context, String id, int type, int action, int level){
        context.startActivity(new Intent(context, SearchSimpleActivity.class)
                .putExtra("id",id)
                .putExtra("searchType",type)
                .putExtra("action",action)
                .putExtra("level",level));
    }
//
//    public static void gotoSearch(Context context, int searchType){
//        gotoSearch(context, searchType, null);
//    }
//    public static void gotoSearch(Context context, int searchType, String action){
//        gotoSearch(context, searchType, action, false);
//    }
//    public static void gotoSearch(Context context, int searchType, String action, boolean showSearchAllBtn){
//        gotoSearch(context, searchType, -1, action, false);
//    }
    public static void gotoSearch(Context context, int searchType, int subType, String action, boolean showSearchAllBtn){
        context.startActivity(new Intent(context, SearchSimpleActivity.class)
                .putExtra("searchType", searchType)
                .putExtra("subType", subType)
                .putExtra("recommendAction", action)
                .putExtra("showSearchAllBtn", showSearchAllBtn));
    }
    public static void gotoSearch(Context context, String webSearchParam){
        context.startActivity(new Intent(context, SearchSimpleActivity.class)
                .putExtra("webSearchParam", webSearchParam));
    }

    @ViewInject(R.id.search_all_btn)
    private TextView searchAllBtn;

    private WebSearchParam webSearchParam;

    @Override
    protected void init() {
        tabView.setVisibility(View.GONE);
        itemNames = new String[]{""};
        try{
            String searchParam = getIntent().getStringExtra("webSearchParam");
            webSearchParam = JsonUtil.getInstance().jsonToObject(searchParam, WebSearchParam.class);
            types = new int[]{webSearchParam.getType()};
        } catch (Exception e) {
            types = new int[]{getIntent().getIntExtra("searchType", 1)};
        }
        super.init();
        searchEdit.setHint("输入关键字搜索" + getSearchTypeStr(types[0]));
        if(fragments != null && webSearchParam != null) {
            for (Fragment fragment : fragments) {
                setSearchData((SearchDetailFragment) fragment,
                        webSearchParam.getAction(),
                        webSearchParam.getId(),
                        webSearchParam.getType());
            }
        } else if(getIntent().getStringExtra("recommendAction") != null){
            for (Fragment fragment : fragments)
                ((SearchDetailFragment)fragment).setAction(getIntent().getStringExtra("recommendAction"));
        }
        for (Fragment fragment : fragments)
            ((SearchDetailFragment)fragment).setSubType(getIntent().getIntExtra("subType", -1));
        if(getIntent().getBooleanExtra("showSearchAllBtn", false)) {
            searchAllBtn.setVisibility(View.VISIBLE);
            searchAllBtn.setText("查看全部" + getSearchTypeStr(types[0]) + " >");
            searchAllBtn.setOnClickListener(this);
        }

        if(webSearchParam == null)
            return;
        application.getApplicationWebView().addJsCallback(new JpApplicationWebView.JsCallback() {
            @Override
            public boolean onJsCallback(JsonObject obj) {
                if("recommendAction".equals(obj.get("type").getAsString())
                        && "goBack".equals(obj.get("param").getAsString())){
                    SearchSimpleActivity.super.finish();
                    return false;
                } else {
                    return true;
                }
            }
        });
    }

    @Override
    protected void showFragment() {
        super.showFragment();
        searchAllBtn.setVisibility(View.GONE);
    }

    @Override
    protected void hideFragment() {
        super.hideFragment();
        if(getIntent().getBooleanExtra("showSearchAllBtn", false))
            searchAllBtn.setVisibility(View.VISIBLE);
    }

    @Override
    public void finish() {
        if(webSearchParam != null)
            application.getApplicationWebView().htmlGoBack();
        else
            super.finish();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if(v.getId() != R.id.search_all_btn)
            return;
        switch (types[0]){
            case 3:     // 公司
                if((webSearchParam != null && "joinExp".equals(webSearchParam.getAction()))
                        || "joinExp".equals(getIntent().getStringExtra("recommendAction")))
                    JpApplicationWebActivity.gotoWebActivity(mContext, "follow/companyList?source=follow&recommendAction=exp");
                else
                    JpApplicationWebActivity.gotoWebActivity(mContext, "follow/companyList?source=follow");
                break;
            case 8:     // 产品
                if((webSearchParam != null && "joinExp".equals(webSearchParam.getAction()))
                        || "joinExp".equals(getIntent().getStringExtra("recommendAction")))
                    JpApplicationWebActivity.gotoWebActivity(mContext, "follow/productList?source=follow&recommendAction=exp");
                else
                    JpApplicationWebActivity.gotoWebActivity(mContext, "follow/productList?source=follow");
                break;
            case 4:     // 人物
                if((webSearchParam != null && "joinExp".equals(webSearchParam.getAction()))
                        || "joinExp".equals(getIntent().getStringExtra("recommendAction")))
                    JpApplicationWebActivity.gotoWebActivity(mContext, "follow/figureList?source=follow&recommendAction=exp");
                else
                    JpApplicationWebActivity.gotoWebActivity(mContext, "follow/figureList?source=follow");
                break;
        }
    }

    @Override
    protected int setSearchBgViewType(){
        return types[0];
    }

    private String getSearchTypeStr(int searchType){
        switch (searchType){
            case 1: return "头条";
            case 2: return "频道";
            case 3: return "公司";
            case 4: return "人物";
            case 5: return "内容";        // 以废除
            case 6: return "竞报";
            case 7: return "方案";
            case 8: return "产品";
            case 9: return "主题";
            case 10: return "体验";
            case 11: return "全民推荐";
            case 12: return "素材";
            case 13: return "知识库";
            case 14: return "报告";
            case 15: return "界面";
            case 16: return "团队圈";
            case 17: return "每日评说";
            case 18: return "内容";       // 关键字
            case 19: return "原型图";
            case 20: return "融资数据";
            case 21: return "行业大观察";
            case 22: return "文库";
            default: return "内容";
        }
    }

    private class WebSearchParam implements Serializable{
        private String id;
        private String action;
        private int type;
        public String getId() {
            return id;
        }
        public void setId(String id) {
            this.id = id;
        }
        public String getAction() {
            return action;
        }
        public void setAction(String action) {
            this.action = action;
        }
        public int getType() {
            return type;
        }
        public void setType(int type) {
            this.type = type;
        }
    }
}

